User
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>

#define BUF_SIZE 100
#define MAX_CLIENTS 10

typedef struct {
    int sock;
    char username[BUF_SIZE];
} ClientInfo;

void error_handling(char* message);
void* handle_client(void* arg);

int main(int argc, char* argv[]) {
    if (argc != 2) {
        printf("Usage: %s <port>\n", argv[0]);
        exit(1);
    }

    int serv_sock = socket(PF_INET, SOCK_STREAM, 0);
    if (serv_sock == -1)
        error_handling("socket() error");

    struct sockaddr_in serv_addr;
    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(atoi(argv[1]));

    if (bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1)
        error_handling("bind() error");

    if (listen(serv_sock, 5) == -1)
        error_handling("listen() error");

    printf("Server started, waiting for connections...\n");

    pthread_t client_threads[MAX_CLIENTS];
    int client_count = 0;

    while (1) {
        struct sockaddr_in clnt_addr;
        socklen_t clnt_addr_size = sizeof(clnt_addr);
        int clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
        if (clnt_sock == -1)
            error_handling("accept() error");

        ClientInfo* client_info = (ClientInfo*)malloc(sizeof(ClientInfo));
        client_info->sock = clnt_sock;

        // 在这里进行认证，你可以根据实际情况实现更严格的认证机制
        strcpy(client_info->username, "UnknownUser");

        // 创建处理客户端的线程
        if (pthread_create(&client_threads[client_count], NULL, handle_client, (void*)client_info) != 0)
            error_handling("pthread_create() error");

        client_count++;
        printf("New client connected, total clients: %d\n", client_count);

        if (client_count >= MAX_CLIENTS) {
            printf("Max clients reached, no longer accepting connections.\n");
            break;
        }
    }

    // 等待所有线程结束
    for (int i = 0; i < client_count; i++) {
        if (pthread_join(client_threads[i], NULL) != 0)
            error_handling("pthread_join() error");
    }

    // 关闭服务器套接字
    close(serv_sock);

    return 0;
}

void* handle_client(void* arg) {
    ClientInfo* client_info = (ClientInfo*)arg;
    int clnt_sock = client_info->sock;

    char auth_message[BUF_SIZE];
    read(clnt_sock, auth_message, sizeof(auth_message));

    // 在这里进行认证，你可以根据实际情况实现更严格的认证机制

    write(clnt_sock, "Auth successful", sizeof("Auth successful"));

    while (1) {
        char message[BUF_SIZE];
        int str_len = read(clnt_sock, message, sizeof(message));
        if (str_len <= 0)
            break;

        // 在这里可以处理接收到的消息，比如转发给其他客户端

        // 示例：简单地将消息原样发送回客户端
        write(clnt_sock, message, str_len);
    }

    printf("Client disconnected\n");

    // 关闭客户端套接字，并释放内存
    close(clnt_sock);
    free(client_info);

    return NULL;
}

void error_handling(char* message) {
    fputs(message, stderr);
    fputc('\n', stderr);
    exit(1);
}